package com.huang.Service.ServiceImpl;

import com.huang.Dao.OrderDao;
import com.huang.Dao.TxLogDao;
import com.huang.Pojo.Order;
import com.huang.Pojo.TxLog;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderServiceImpl4 {

    @Resource
    private OrderDao orderDao;

    @Resource
    private TxLogDao txLogDao;

    @Resource
    private RocketMQTemplate rocketMQTemplate;


    public void createOrderBefore(Order order){
        String txId = UUID.randomUUID().toString();
        rocketMQTemplate.sendMessageInTransaction("tx_producer_group", MessageBuilder.withPayload(order).setHeader("txId", txId).build(), order);
    }

    @Transactional
    public void createOrder(String txId,Order order){
        orderDao.save(order);
        TxLog txLog = new TxLog();
        txLog.setTxId(txId);
        txLog.setDate(new Date());
        txLogDao.save(txLog);
    }


}
